package com.runen.infinitemovement.tool;

import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;

import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationManagerCompat;

import com.runen.infinitemovement.App;
import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.widget.popupwind.CommonDialog;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class NotificationsUtils {

    private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
    private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

    @SuppressLint("NewApi")
    public static boolean isNotificationEnabled(Context context) {
        AppOpsManager mAppOps =
                (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        Class appOpsClass = null;
        /* Context.APP_OPS_MANAGER */
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());

            Method checkOpNoThrowMethod =
                    appOpsClass.getMethod(CHECK_OP_NO_THROW,
                            Integer.TYPE, Integer.TYPE, String.class);

            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
            int value = (Integer) opPostNotificationValue.get(Integer.class);

            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) ==
                    AppOpsManager.MODE_ALLOWED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void openNotification(Context context) {
        if (!NotificationsUtils.isNotificationEnabled(context)) {
            CommonDialog.getInstance(context, "", "检测到您没有打开通知权限，是否去打开", "取消", "确定", 0).setOnClickListener(new CommonDialog.OnClickListener() {
                @Override
                public void onSendListener() {

                }

                @Override
                public void onDismiss() {
                    Intent localIntent = new Intent();
                    //判断API，跳转到应用通知管理页面
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0及以上
                        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                        localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
                    } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//8.0以下
                        localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                        localIntent.putExtra("app_package", context.getPackageName());
                        localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
                    }
                    context.startActivity(localIntent);
                }
            });

        }
    }

}